/**
 * 
 */
$(function () {
	$("#modify_resume").bind("click",function () { //导航栏用户点击简历时激发的函数
		$(".modify_picturediv").css("display","none");
		$(".modif_passworddiv").css("display","none");
		$(".complaindiv").css("display","none");
		$(".become_businessdiv").css("display","none");
		$(".modify_resumediv").css("display","block");
		show_personal_resume();
	})
	
	function show_personal_resume() {
		var strhtml = "";
		$.ajax({    // 将数据发送到服务器
            url:"login.html",  //等待接收用户电话号码的文件地址
            data: { 
            	user_phone: $("#user_phone").val(), //登陆用户的电话号码，待定
            },
            dataType:"json",
            type:"post",
            success:function(data) {
            	strhtml += "姓名：" + data.u_name +"<br><br>"; 
            	strhtml += "性别：" + data.sex +"<br><br>"; 
            	strhtml += "电话：" + data.phone +"<br><br>"; 
            	strhtml += "邮箱：" + data.E_mail +"<br><br>";
            	strhtml += "学历：" + data.education +"<br><br>"; 
            	strhtml += "工作经历：" + data.experience +"<br><br>"; 
            	strhtml += "自我介绍：" + data.self_introduction +"<br><br>"; 
            	$("#show_resume_content").html(strhtml);
            	$("#user_name").val(data.u_name);  
            	$('#user_form input[name = "user_sex"]').val(data.sex);  
            	$("#user_email").val(data.E_mail);  
            	$("#user_phone").val(data.phone);  
            	$("#education option").text(data.education); 
            	$("#user_work_experience").val(data.experience);  
            	$("#user_introduce").val(data.self_introduction); 
			},
			error:function(request) {
				alert("获取失败！");
			}
        })
	}
	
	$("#edit").click(function () {
		$("#edit_resume").css("display","block");
		$("#show_resume").css("display","none");
	})
	
	$("#user_register_btn").click(function () { //修改简历后点击保存按钮触发的函数
		var reg_value = {};					//正则表达式数组
		reg_value["user_name"] =/^([\u4e00-\u9fa5]+|([a-z]+\s?)+)$/;
		reg_value["user_email"] = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/ ;
		reg_value["user_phone"] = /^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$|(^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])\d{8}$)/;
		var eles,ele_val,err=0;
		eles = $("#edit_resume").find("input,textarea");
		eles.each(function () {
			ele_val = $(this).val();
			var  number = $(this).attr("tabindex");
			if(number == 1) {		//姓名输入框规则
				if(ele_val == null || ele_val == "") {
					$("#user_name + span").html("姓名不能为空");
					err++;
				}
				else if(!reg_value["user_name"].test(ele_val)) {
					$("#user_name + span").html("姓名格式不正确");
					err++;
				}
				else {
					$("#user_name + span").html("√");
				}
			}
			else if (number == 2) {		//邮箱地址输入框规则
				if(ele_val != null && ele_val != "") {
					if(!reg_value["user_email"].test(ele_val)) {
						$("#user_email + span").html("邮箱格式不正确");
						err++;
					}
					else{
						$("#user_email + span").html("√");
					}
				}
				else{
					$("#user_email + span").html("");
				}
				
			}
			else if(number == 3) {		//电话号码输入框规则
				if(ele_val == null || ele_val == "") {
					$("#user_phone + span").html("电话号码不能为空");
					err++;
				}
				else if(!reg_value["user_phone"].test(ele_val)) {
					$("#user_phone + span").html("电话号码格式不正确");
					err++;
				}
				else {
					$("#user_phone + span").html("√");
				}
			}
			else if(number == 4) {		//工作经历输入框规则
				if(ele_val == null || ele_val == "") {
					$("#user_span1").html("工作经历不能为空");
					err++;
				}
				else{
					$("#user_span1").html("√");
				}
			}
			else if(number == 5) {		//自我介绍输入框规则
				if(ele_val == null || ele_val == "") {
					$("#user_span2").html("自我介绍不能为空");
					err++;
				}
				else{
					$("#user_span2").html("√");
				}
			}
		})
		if(!err) {		//没有出现错误时的处理
		$.ajax({    // 将数据发送到服务器
            url:"login.html",  //等待接收新密码的文件地址
            data: { 
            	user_name: $("#user_name").val(),   //用户姓名
            	user_sex: $('#user_form input[name = "user_sex"]:checked').val(), //性别
            	user_email: $("#user_email").val(),  //邮箱
            	user_phone: $("#user_phone").val(), //电话号码
            	education: $("#education option:selected").text(), //学历
            	user_work_experience: $("#user_work_experience").val(), //工作经历
            	user_introduce: $("#user_introduce").val() // 自我介绍
            },
            dataType:"json",
            type:"post",
            success:function(data) {
            	if(data) {
            		alert("保存成功！");
            		show_personal_resume();
            		$("#edit_resume").css("display","none");
        			$("#show_resume").css("display","block");
           		 }
            	else{
            		alert("保存失败！");
            		
            	}
			},
			error:function(request) {
				alert("保存失败！");
			}
        })
	}
	else{
		alert("填写信息错误！");
	}
	})
	
	$("#business_cancel_btn").click(function () {
		var result = confirm("确认放弃修改？");
		if(result) {
			$("#edit_resume").css("display","none");
			$("#show_resume").css("display","block");
		}
	})
	
	$("#modify_picture").bind("click",function () { //导航栏用户点击修改头像时激发的函数
		$(".modify_resumediv").css("display","none");
		$(".complaindiv").css("display","none");
		$(".modif_passworddiv").css("display","none");
		$(".become_businessdiv").css("display","none");
		$(".modify_picturediv").css("display","block");
	})
	
	$("#modif_picturebtn").change(function () {  //用户点击预览按钮是激发的函数
		var path = $("#modif_picturebtn").val();
		$("#picturediv img").attr("src",path);
	})
	
	$("#upload_picture").click(function () {  // 用户点击上传头像时激发的函数
		var path = $("#modif_picturebtn").val();
		$("#head_portrait").attr("src",path);
	})
	
	$("#modify_password").bind("click",function () {  //导航栏用户点击修改密码时激发的函数
		$(".modify_resumediv").css("display","none");
		$(".complaindiv").css("display","none");
		$(".modify_picturediv").css("display","none");
		$(".become_businessdiv").css("display","none");
		$(".modif_passworddiv").css("display","block");
	})
	
	$("#password_submit").click(function () {  //密码修改点击确认时激发的函数
		var eles,ele_val,err=0;
		eles = $(".modif_passworddiv").find("input");
		eles.each(function () {
			ele_val = $(this).val();
			var  number = $(this).attr("tabindex");
			if(number == 1) {		                  //旧密码输入框规则
				if(ele_val == null || ele_val == "") {
					$("#old_password + span").html("旧密码不能为空");
					err++;
				}
				else {
					$("#old_password + span").html("√");
				}
			}
			else if(number == 2) {		                 //新密码输入框规则
				if(ele_val == null || ele_val == "") {
					$("#new_password + span").html("新密码不能为空");
					err++;
				}
				else {
					$("#new_password + span").html("√");
				}
			}
			else if(number == 3) {		               //确认密码输入框规则
				if(ele_val == null || ele_val == "") {
					$("#new_password2 + span").html("确认密码不能为空");
					err++;
				}
				else if($("#new_password").val() != $("#new_password2").val()) {
					$("#new_password2 + span").html("两次密码不一致");
					err++;
				}
				else{
					$("#new_password2 + span").html("√");
				}
			}
		})
		if(!err) {		//没有出现错误时的处理
			$.ajax({    // 将新密码发送到服务器
	            url:"login.html",  //等待接收新密码的文件地址
	            data: { 
	            	old_password: $("#old_password").val(),	//原始密码
	            	new_password: $("#new_password").val()   //缺少传入的电话号码
	            },
	            dataType:"json",
	            type:"post",
	            success:function(data) {
	            	if(data) {
	            		alert("密码修改成功，请重新登陆");
	            		location.href = "login.html";
	           		 }
	            	else{
	            		alert("修改密码失败！");
	            		
	            	}
				},
				error:function(request) {
					alert("修改密码失败！");
				}
	        })
		}
		else{
			alert("填写信息错误！");
		}
	})
	
	$("#password_resetbtn").click(function () {
		$("#old_password + span").html("*");
		$("#new_password + span").html("*");
		$("#new_password2 + span").html("*");
	})
	
	$("#complain").bind("click",function () {  //导航栏用户投诉点击函数
		$(".modify_resumediv").css("display","none");
		$(".modify_picturediv").css("display","none");
		$(".modif_passworddiv").css("display","none");
		$(".become_businessdiv").css("display","none");
		$(".complaindiv").css("display","block");
	})
	
	$("#submitbtn").click(function () {   // 用户投诉点击提交时激发的函数
		var eles,ele_val,err=0;
		eles = $(".complaindiv").find("input,textarea");
		eles.each(function () {
			ele_val = $(this).val();
			var  number = $(this).attr("tabindex");
			if(number == 1) {		//反馈信息输入框规则
				if(ele_val == null || ele_val == "") {
					$("#complain_span").html("反馈信息不能为空");
					err++;
				}
				else {
					$("#complain_span").html("√");
				}
			}
		})
		
		if(!err) {		//没有出现错误时的处理
			$.ajax({    // 将新密码发送到服务器
	            url:"login.html",  //等待接收新密码的文件地址
	            data: { 
	            	words: $("#words").val()  //反馈信息
	            },
	            dataType:"json",
	            type:"post",
	            success:function(data) {
	            	if(data) {
	            		alert("提交成功！");
	           		 }
	            	else{
	            		alert("提交失败！");
	            		
	            	}
				},
				error:function(request) {
					alert("提交失败！");
				}
	        })
		}
		else{
			alert("填写信息错误！");
		}
	})
	
	$("#resetbtn").click(function () {
		$("#complain_span").html("*");
	})
	
	$("#become_business").bind("click",function () {  //导航栏点击申请成为商家函数
		$(".modify_resumediv").css("display","none");
		$(".modify_picturediv").css("display","none");
		$(".modif_passworddiv").css("display","none");
		$(".complaindiv").css("display","none");
		$(".become_businessdiv").css("display","block");
	})
	
	$("#business_register_btn").click(function () {  //申请商家注册按钮点击函数
		var reg_value = {};					//正则表达式数组
		reg_value["business_id"]  = /^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$|(^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])\d{8}$)/;
		reg_value["business_email"] = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/ ;
		var eles,ele_val,err=0;
		eles = $(".become_businessdiv").find("input,textarea");
		eles.each(function () {
			ele_val = $(this).val();
			var  number = $(this).attr("tabindex");
			if(number == 1) {		//公司名称输入框规则
				if(ele_val == null || ele_val == "") {
					$("#business_name + span").html("公司名称不能为空");
					err++;
				}
				else{
					$("#business_name + span").html("√");
				}
			}
			else if(number == 2) {	//密码输入框规则
				if(ele_val == null || ele_val == "") {
					$("#business_password + span").html("密码不能为空");
					err++;
				}
				else{
					$("#business_password + span").html("√");
				}
			}
			else if(number == 3) {		//确认密码输入框规则
				if(ele_val == null || ele_val == "") {
					$("#business_password2 + span").html("确认密码不能为空");
					err++;
				}
				else if($("#business_password").val() != $("#business_password2").val()) {
					$("#business_password2 + span").html("两次密码不一致");
					err++;
				}
				else{
					$("#business_password2 + span").html("√");
				}
			}
			else if (number == 4) {		//邮箱地址输入框规则
				if(ele_val != null && ele_val != "") {
					if(!reg_value["business_email"].test(ele_val)) {
						$("#business_email + span").html("邮箱格式不正确");
						err++;
					}
					else{
						$("#business_email + span").html("√");
					}
				}
				else {
					$("#business_email + span").html("");
				}
				
			}
			else if(number == 5) {		//电话号码输入框规则
				if(ele_val == null || ele_val == "") {
					$("#business_phone + span").html("电话号码不能为空");
					err++;
				}
				else if(!reg_value["business_id"].test(ele_val)) {
					$("#business_phone + span").html("电话号码格式不正确");
					err++;
				}
				else{
					$("#business_phone + span").html("√");
				}
			}
			else if(number == 6) {		//许可证输入框规则
				if(ele_val == null || ele_val == "") {
					$("#business_licence + span").html("许可证不能为空");
					err++;
				}
				else{
					$("#business_licence + span").html("√");
				}
			}
		})
		if(!err) {		//没有出现错误时的处理
			$.ajax({    // 将数据发送到服务器
	            url:"login.html",  //等待接收新密码的文件地址
	            data: { 
	            	business_name: $("#business_name").val(),  //公司名称
	            	business_password: $("#company_id").val(), //密码
	            	business_email: $("#business_email").val(), //邮箱
	            	business_phone: $("#business_phone").val(), //电话号码
	            	business_licence: $("#business_licence").val(), //公司许可证
	            	business_introduce: $("#business_introduce").val() //公司简介
	            },
	            dataType:"json",
	            type:"post",
	            success:function(data) {
	            	if(data) {
	            		alert("申请成功！");
	           		 }
	            	else{
	            		alert("申请失败！");
	            		
	            	}
				},
				error:function(request) {
					alert("申请失败！");
				}
	        })
		}
		else{
			alert("填写信息错误！");
		}
	})
})